package com.elitesland.system.service.impl;

import com.elitesland.system.annotation.SysCodeProc;
import com.elitesland.system.convert.SysNumberRuleDtlConvert;
import com.elitesland.system.entity.SysNumberRuleDtlDO;
import com.elitesland.system.repo.SysNumberRuleDtlRepo;
import com.elitesland.system.service.SysNumberRuleDtlService;
import com.elitesland.system.vo.SysNumberRuleDtlVO;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <pre></pre>
 *
 * @author mir
 * @date 2020-10-11
 */
@Service
public class SysNumberRuleDtlServiceImpl implements SysNumberRuleDtlService {

    private SysNumberRuleDtlRepo sysNumberRuleDtlRepo;

    @Autowired
    public void setSysNumberRuleDtlRepo(SysNumberRuleDtlRepo sysNumberRuleDtlRepo) {
        this.sysNumberRuleDtlRepo = sysNumberRuleDtlRepo;
    }

    @Override
    @SysCodeProc
    public List<SysNumberRuleDtlVO> listByRuleId(Long ruleId) {
        val details = sysNumberRuleDtlRepo.findAllByRuleIdOrderBySeq(ruleId);
        return details.stream().sorted(Comparator.comparing(SysNumberRuleDtlDO::getSeq))
                .map(SysNumberRuleDtlConvert.INSTANCE::doToVO)
                .collect(Collectors.toList());
    }

    @Override
    public Optional<SysNumberRuleDtlVO> oneById(Long id) {
        return sysNumberRuleDtlRepo.findById(id).map(SysNumberRuleDtlConvert.INSTANCE::doToVO);
    }

    @Override
    @Transactional(rollbackOn = {Exception.class})
    public void updateInBatch(List<SysNumberRuleDtlVO> details) {
        val oldDetails = sysNumberRuleDtlRepo.findAllByRuleIdOrderBySeq(details.get(0).getRuleId());
        if (!oldDetails.isEmpty()) {
            sysNumberRuleDtlRepo.deleteAllByRuleId(details.get(0).getRuleId());
        }

//        var seq = 1;
//        for (val d : details) {
//            d.setSeq(seq);
//            seq++;
//        }
        sysNumberRuleDtlRepo.saveAll(
                //过滤规则ID不大于0的无效规则明细
                details.stream().filter(d -> d.getRuleId() > 0L)
                        .map(SysNumberRuleDtlConvert.INSTANCE::voToDO)
                        .collect(Collectors.toList())
        );
    }

    @Override
    @Transactional(rollbackOn = {Exception.class})
    public void removeByIds(List<Long> ids) {
        val oldDetails = sysNumberRuleDtlRepo.findAllByIdIn(ids);
        if (oldDetails.isEmpty()) {
            return;
        }

        sysNumberRuleDtlRepo.deleteAllByIdIn(ids);

        val details = sysNumberRuleDtlRepo.findAllByRuleIdOrderBySeq(oldDetails.get(0).getRuleId());
        var seq = 1;
        for (val d : details) {
            d.setSeq(seq);
            seq++;
        }
        sysNumberRuleDtlRepo.saveAll(details);
    }

    @Override
    public void removeByRuleId(Long ruleId) {
        sysNumberRuleDtlRepo.deleteAllByRuleId(ruleId);
    }
}
